import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:xin_he/page/mine/xin_he_bank_management/xin_he_bank_list_sheet/xin_he_bank_code_entity.dart';
import 'package:xin_he/page/mine/xin_he_bank_management/xin_he_bank_list_sheet/xin_he_bank_list_sheet_view.dart';

import '../../../../utils/xh_styles.dart';
import '../../../../utils/xin_he_image.dart';
import 'xin_he_add_bank_logic.dart';

class XinHeAddBankPage extends StatelessWidget {
  XinHeAddBankPage({Key? key}) : super(key: key);

  final logic = Get.put(XinHeAddBankLogic());
  final state = Get.find<XinHeAddBankLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        leading: IconButton(
            onPressed: (){
              Get.back();
            },
            icon: Container(
              padding: const EdgeInsets.only(left: 7),
              alignment: Alignment.centerLeft,
              child: const XinHeAssetImage(
                'assets/images/home/app_back_3.png',
                width: 9.5,
                height: 17,
                fit: BoxFit.cover,),
            )
        ),
        title:  Text((logic.editModel.name??'').isNotEmpty?"编辑银行卡":'添加银行卡',style: const TextStyle(color: Colors.black , fontSize: 18,fontWeight: FontWeight.w600),),
        backgroundColor: Colors.white,
        shadowColor: Colors.transparent,
      ) ,
      body: Obx(() => Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [


          _itemViews('持卡人', '请输入持卡人姓名   ', (data) { state.name.value = data;},isShowLine: false,controller: logic.nameController),
          // _itemViews('发卡行', '请选择发卡行', (data) { state.bankName.value = data; },controller: logic.bankNameController),

          Stack(children: [
            GestureDetector(
              onTap: () async{
                // Get.bottomSheet(XinHeBankListSheetPage(),persistent: false,isScrollControlled: true);

                var model = await  Get.bottomSheet(XinHeBankListSheetPage(),persistent: false,isScrollControlled: true);
                logic.bankCodeModel.value = model;
              },
              child: Container(
                height: 45,
                alignment: Alignment.centerLeft,
                color: Colors.white,
                padding: const EdgeInsets.symmetric(horizontal: 15),
                child: Row(children: [
                  Text('发卡行',style: TextStyle(color: XinHeColor('#222222'),fontSize: 14,fontWeight: FontWeight.bold),),
                  Spacer(),
                  Text((logic.bankCodeModel.value.name??'').isNotEmpty? logic.bankCodeModel.value.name!: '请选择发卡行',style: TextStyle(color: XinHeColor("#999999"),fontSize: 14),),

                  // const SizedBox(width: 5+6,),

                ],),),
            ),
              Container(width: Get.width,height: 0.5,color: XinHeStyle.lineColor,)
          ],),
          _itemViews('银行卡号', '请输入银行卡号', (data) { state.bankCard.value = data;},keyboardType: TextInputType.number,maxLength: 20,controller:logic.bankCardController),
          // _regionItemView(),
          _defaultItemView(),

          Spacer(),
          ///保存
          Container(
            margin:   EdgeInsets.only(left: 15,right: 15,bottom: Get.mediaQuery.padding.bottom+25),
            height: 44,
            width: double.infinity,
            decoration: BoxDecoration(color:XinHeColor('#30E9E9'),borderRadius: const BorderRadius.all(Radius.circular(22))),
            child: OutlinedButton(

              onPressed: (){
                logic.saveAddress();
              },
              child:  const Text (
                "保存",
                style: TextStyle (fontSize: 16,color:  Colors.white, fontWeight: FontWeight.bold) ,
                strutStyle: StrutStyle(
                  fontSize: 16,
                  leading: 0,
                  height: 1.1,
                  // 1.1更居中
                  forceStrutHeight: true, // 关键属性 强制改为文字高度
                ),
              ),
            ),),
          SizedBox(height: Get.mediaQuery.padding.bottom+25,)

        ],
      )),

    );
  }

  Widget _itemViews(String title,String hintText, Function(String) callback,{bool isShowLine = true,TextInputType keyboardType=TextInputType.text ,int maxLength = 1000 ,TextEditingController ?controller}){
    return    Stack(children: [
      Container(
        height: 45,
        alignment: Alignment.centerLeft,
        color: Colors.white,
        padding: const EdgeInsets.symmetric(horizontal: 15),
        child: Row(children: [
          Text(title,style: TextStyle(color: XinHeColor('#222222'),fontSize: 14,fontWeight: FontWeight.bold),),
          Spacer(),
          Container(
            width: 160,
            alignment: Alignment.centerRight,
            child: TextField(
              // maxLength: maxLength,
              controller: controller??TextEditingController(),
              maxLines: 1,
              keyboardType: keyboardType,
              textAlign: TextAlign.right,
              onChanged: (value){
                callback(value);
              },
              style:  TextStyle(
                  color: XinHeColor("#15161F"), fontSize: 14 ,fontWeight: FontWeight.w400),
              decoration:  InputDecoration(
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.zero,
                  hintText: hintText,
                  counterStyle: TextStyle(
                      color: XinHeColor("#999999"), fontSize: 14),
                  hintStyle:  TextStyle(
                      color: XinHeColor("#999999"), fontSize: 14)),
              inputFormatters: [
                LengthLimitingTextInputFormatter(maxLength),

              ],
            ),
          ),
          // const SizedBox(width: 5+6,),

        ],),),
      isShowLine?  Container(width: Get.width,height: 0.5,color: XinHeStyle.lineColor,):Container()
    ],);
  }

  Widget _defaultItemView(){
    return Stack(children: [
      Container(
        padding: const EdgeInsets.symmetric(horizontal: 15),
        height: 45,
        color: Colors.white,
        child: Row(children: [
          Text('设为默认地址',style: TextStyle(color: XinHeColor('#222222'),fontSize: 14,fontWeight: FontWeight.bold),),
          Spacer(),
          GestureDetector(
            onTap: (){
              state.isDefault.value = !state.isDefault.value;
            },
            child: Container(
                padding: const EdgeInsets.symmetric(vertical: 8).copyWith(left: 8),
                color: Colors.transparent,
                child: XinHeAssetImage( state.isDefault.value?'assets/images/cart/cart_selected.png':'assets/images/cart/cart_unselected.png',width:15 ,height: 15,)),
          )

        ],),
      ),
      Container(width: Get.width,height: 0.5,color: XinHeStyle.lineColor,)
    ],);
  }

}
